﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SpaceInvaders_LeonardoCamelo
{
    class Player : GameObject //Classe de jogador, deriva de GameObject.
    {
        private Vector2 speed; //Vetor de velocidade.
        private bool shotAble; //Define se pode atirar ou não.
        public List<Bullet> bulletList; //Lista de projeteis.

        //Dentro do construtor igualamos os parâmetros aos atributos que serão iniciados com o objeto.
        public Player(Rectangle rectanglePicture, Texture2D texture, Vector2 speed)
        {
            this.rectanglePicture = rectanglePicture;
            this.texture = texture;
            this.speed = speed;
            this.bulletList = new List<Bullet>();
        }

        //Metodo de atualizações.
        public void Update(KeyboardState keyNow, int WIDTH, int HEIGHT)
        {
            //Se teclar para esquerda, vá para esquerda.
            if (keyNow.IsKeyDown(Keys.Left))
                this.rectanglePicture.X -= (int)speed.X;
            //Se teclar para direita, vá para direita.
            if (keyNow.IsKeyDown(Keys.Right))
                this.rectanglePicture.X += (int)speed.X;
            ////Se teclar para cima, vá para cima.
            //if (keyNow.IsKeyDown(Keys.Up))
            //    this.rectanglePicture.Y -= (int)speed.Y;
            ////Se teclar para baixo, vá para baixa.
            //if (keyNow.IsKeyDown(Keys.Down))
            //    this.rectanglePicture.Y += (int)speed.Y;

            //Impede que o personagem saia da tela.
            if (this.rectanglePicture.Right > WIDTH)
                this.rectanglePicture.X -= (int)speed.X;
            if (this.rectanglePicture.Left < 0)
                this.rectanglePicture.X += (int)speed.X;
            //if (this.rectanglePicture.Top < 0)
            //    this.rectanglePicture.Y += (int)speed.Y;
            //if (this.rectanglePicture.Bottom > HEIGHT)
            //    this.rectanglePicture.Y -= (int)speed.Y;
        }

        //Metodo de tiros.
        public void Shot(KeyboardState keyNow, Bullet bullet)
        {
            //Se puder atirar.
            if (this.shotAble)
            {
                //Se teclar espaço.
                if (keyNow.IsKeyDown(Keys.Space))
                {
                    //Adicionamos mais um projetil.
                    bulletList.Add(new Bullet(new Rectangle(this.rectanglePicture.X + 45, this.rectanglePicture.Y,
                                                            bullet.rectanglePicture.Width, bullet.rectanglePicture.Height),
                                                            bullet.texture, bullet.speed));
                    //Impedimos de atirar.
                    this.shotAble = false;
                }
            }
            //Se largar a tecla espaço.
            if (keyNow.IsKeyUp(Keys.Space))
                //Pode atirar.
                this.shotAble = true;
        }
    }
}
